package com.lrkj.back.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.entity.*;
import com.lrkj.back.service.*;
import com.lrkj.back.vo.constant.PathInfo;
//import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import cn.com.cbons.mamisay.parent.exception.BusinessException;
import springfox.documentation.spring.web.json.Json;

@Controller
@RequestMapping("/peibao")
@Transactional()
public class PeiBaoController extends BaseController {
    @Autowired
    private ZsQingxiService zsQingxiService;

    @Autowired
    private ZsQingxiMingxiService zsQingxiMingxiService;

    @Autowired
    private ZsHuishouService zsHuishouService;

    @Autowired
    private ZsHuishouMingxiService zsHuishouMingxiService;

    @Autowired
    private ZsPeibaoService zsPeibaoService;

    @Autowired
    private TbSeqService seqService;

    @Autowired
    private ZsDabaoService zsDabaoService;

    @Autowired
    private ZsHuishouQueshiService zsHuishouQueshiService;

    @Autowired
    private ZsHuishouWutiaomaService zsWutiaomaService;

    @Autowired
    private TbDepartmentService tbDepartService;

    @Autowired
    private TbPackageMubanService tbPackageMubanService;

    @Autowired
    private TbProductService tbProductService;

    @Autowired
    private ZsWanglanpackageService zsWlpackService;

    @Autowired
    private ZsPackageImformationService zsPackageImService;

    @Autowired
    private TbEmployeeService tbEmployeeService;


    @RequestMapping("/findQixieByBarCode")
    @ResponseBody
    public Map<String, Object> qixieList(String pdata, String barCode, String baoId) {
        Map<String, Object> rtnMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        List<TbProduct> page = new ArrayList<>();
        if (StringUtils.isNotBlank(pdata)) {
            JSONObject jsonObject = JSONObject.parseObject(pdata);
            barCode = jsonObject.getString("barCode");
        }

        if (StringUtils.isNotBlank(barCode)) {
            ZsPackageImformation zsPackageImformation = new ZsPackageImformation();
            if (barCode != null && !"".equals(barCode)) {
                zsPackageImformation = zsPackageImService.selectByPrimaryKey(barCode);
            }
            baoId = zsPackageImformation.getTbPackageMuban().getPackageId();
            map.put("packageId", baoId);
            page = tbProductService.findQixieByPackage(map);
        }

        rtnMap.put("data", page);
        return rtnMap;
    }


    @RequestMapping("/peibaoListAndroid")
    @ResponseBody
    public Map<String, Object> peibaoList(String pdata, String type) {
        Map<String, Object> rtnMap = new HashMap<>();
        if (StringUtils.isNotBlank(pdata)) {
            JSONObject jsonObject = JSONObject.parseObject(pdata);
            type = jsonObject.getString("type");
        }
        int way = type == null ? 0 : Integer.valueOf(type);
        Map<String, Object> map = new HashMap<>();
        int[] hegeArr;
        List<ZsPeibao> page = new ArrayList<>();
        switch (way) {
            case 0:
                break;
            case 1:
                map.put("packageLiuchengZhuangtai", 3);
                map.put("waitPeibao", true);
                List<ZsPackageImformation> zsPackageImformations = zsPackageImService.selectSin(map);
                List<ZsPeibao> zsPeibaos = new ArrayList<>();
                for (int i = 0; i < zsPackageImformations.size(); i++) {
                    //可能有空指针
                    ZsPeibao zsPeibao = new ZsPeibao();
                    List<ZsWanglanpackage> list = zsPackageImformations.get(i).getZsWanglanpackage();
                    ZsWanglanpackage zsWanglanpackage = list.get(0);
                    zsPeibao.setQingxiWanglanTiaoma(zsWanglanpackage.getWanglanTiaoma());
                    zsPeibao.setPeibaoBaozhuangTiaoma(zsPackageImformations.get(i).getPackageTiaoma());
                    zsPeibao.setQingxiWupinbaoMingcheng(zsPackageImformations.get(i).getPackageName());
                    zsPeibao.setQingxiLiushui(String.valueOf(zsPackageImformations.get(i).getPackageId()));
                    zsPeibaos.add(zsPeibao);
                }
                rtnMap.put("datas",zsPeibaos);
                break;
            case 2:
                map.put("peibaoZhuangtai", 0);
                hegeArr = new int[]{0, 2};
                map.put("peibaoHege", hegeArr);
                page = zsPeibaoService.findAllZsPeibaoNoPage(map);
                rtnMap.put("datas", page);
                break;
            case 3:
                map.put("peibaoZhuangtai", 0);
                hegeArr = new int[]{1};
                map.put("peibaoHege", hegeArr);
                page = zsPeibaoService.findAllZsPeibaoNoPage(map);
                rtnMap.put("datas", page);
                break;
            default:
        }

        return rtnMap;
    }


    @RequestMapping("/qingxiList.do")
    public ModelAndView qingxiList(
            String searchTxt, String st, String et, HttpServletRequest request, HttpServletResponse response
    ) throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1
                : Integer.valueOf(request.getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        if (StringUtils.isNotBlank(searchTxt)) {
            map.put("searchTxt", searchTxt);
        }
        try {
            if (StringUtils.isNotBlank(st)) {
                startTime = sdf.parse(st);
                map.put("startTime", startTime);
            }
            if (StringUtils.isNotBlank(et)) {
                endTime = sdf.parse(et);
                map.put("endTime", endTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        map.put("qingxiZhuangtai", 2);
        map.put("qingxiHege", 1);
        map.put("packageFlag", 1);
        PageInfo<ZsQingxi> page = zsQingxiService.findAllZsQingxi(map);
        request.setAttribute("qingxiList", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        request.setAttribute("emMap", StartUpOnLoadData.emMap);
        request.setAttribute("equipMap", StartUpOnLoadData.equipMap);
        request.setAttribute("softMap", StartUpOnLoadData.softMap);
        return new ModelAndView("/peibao/peibao");
    }

    @RequestMapping("/peibaoMingxiList.do")
    @ResponseBody
    public Map<Object, Object> peibaoMingxiList(String id)
            throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        Map<Object, Object> rtnMap = new HashMap<>();
        map.put("qingxiLiushui", id);
        try {
            List<ZsWanglanpackage> zsWanglanpacklist = zsWlpackService.findAllWlpackListNoPage(map);
            rtnMap.put("zsWanglanpacklist", zsWanglanpacklist);
            rtnMap.put("status", "true");
            rtnMap.put("msg", "查询成功");
        } catch (Exception e) {
            rtnMap.put("status", "false");
            rtnMap.put("msg", "查询失败");
            e.printStackTrace();
        }
        return rtnMap;
    }


    // 生成配包的数据
    @RequestMapping("/peibaobaocun.do")
    @ResponseBody
    public Map<Object, Object> peibaobaocun(String pdata, String guanbaoren) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dt = new Date();
        try {
            String list = request.getParameter("dataList");
            int leng = 0;
            String peibaoren ="";
            String shenheren = "";
            String [] packageId = new String[leng];
            String[] wlArr = new String[leng];
            String[] packageTiaoma = new String[leng];
            String [] packageName = new String [leng];
            String [] peibaorenArr = new String [leng];
            if(StringUtils.isNotBlank(list)){
                JSONArray jsonArray = JSON.parseArray(list);
                peibaoren = jsonArray.getJSONObject(0).getString("peibaoren");
                shenheren = jsonArray.getJSONObject(0).getString("shenheren");
                leng = jsonArray.size();
               packageId = new String[leng];
                wlArr = new String[leng];
                packageTiaoma = new String[leng];
                packageName = new String [leng];
                peibaorenArr = new String [leng];
                for (int i = 0; i < leng; i++) {
                    JSONObject jo = jsonArray.getJSONObject(i);
                    packageId[i] = jo.getString("packageId");
                    packageTiaoma[i] = jo.getString("packageTiaoma");
                    packageName[i] = jo.getString("packageName");
                    peibaorenArr[i] = jo.getString("peibaoren").toUpperCase();
                    wlArr[i] = jo.getString("wanglanTiaoma");
                }
            }

            if(pdata!=null){
                JSONObject jsonObject = JSONObject.parseObject(pdata);
                peibaoren = jsonObject.getString("peoplecode");
                shenheren = jsonObject.getString("shenheren");
                guanbaoren = jsonObject.getString("guanbaoren");
                JSONArray jsonArray1 = jsonObject.getJSONArray("datas");
                leng = jsonArray1.size();
                packageId = new String[leng];
                wlArr = new String[leng];
                packageTiaoma = new String[leng];
                packageName = new String [leng];
                peibaorenArr = new String [leng];
                for (int i = 0; i < leng; i++) {
                    JSONObject jo = jsonArray1.getJSONObject(i);
                    packageId[i] = jo.getString("qingxiLiushui");
                    packageTiaoma[i] = jo.getString("peibaoBaozhuangTiaoma");
                    packageName[i] = jo.getString("qingxiWupinbaoMingcheng");
                    peibaorenArr[i] = peibaoren.toUpperCase();
                    wlArr[i] = jo.getString("qingxiWanglanTiaoma");
                }
            }
            TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(peibaoren);
            TbEmployee tbEmployee2 = tbEmployeeService.selectByEmployeeNo(shenheren);
            TbEmployee tbEmployee3 = tbEmployeeService.selectByEmployeeNo(guanbaoren);
            if (tbEmployee != null && tbEmployee2 != null && tbEmployee3 != null) {
                Long[] pbls = seqService.seqValBatch("peibaono", leng);
                List<ZsPeibao> pbList = new ArrayList<>();
                for (int i = 0; i < leng; i++) {
                    ZsPeibao zsPeibao = new ZsPeibao();
                    String peibaoLiushui = "PB" + sdf.format(dt) + pbls[i];
                    zsPeibao.setPeibaoLiushui(peibaoLiushui);
                    zsPeibao.setPeibaoTime(dt);
                    zsPeibao.setPeibaoZhuangtai(2);
                    zsPeibao.setQingxiLiushui(packageId[i]);
                    zsPeibao.setPeibaoBaozhuangTiaoma(packageTiaoma[i]);
                    zsPeibao.setQingxiWanglanTiaoma(wlArr[i]);
                    zsPeibao.setQingxiWupinbaoMingcheng(packageName[i]);
                    zsPeibao.setPeibaoPiebaorenTiaoma(peibaorenArr[i]);
                    zsPeibao.setRecordTime(dt);
                    zsPeibao.setPeibaoShenherenTiaoma(shenheren.toUpperCase());
                    zsPeibao.setPeibaoGuanbaoTiaoma(guanbaoren.toUpperCase());
                    zsPeibao.setPeibaoShenherenTime(dt);
                    zsPeibao.setPeibaoGuanbaoTime(dt);
                    zsPeibao.setPeibaoHege(1);
                    pbList.add(zsPeibao);
                }

                zsPeibaoService.insertBatch(pbList);

                // 更新网篮对应包状态
                updateWlPackStatus(packageTiaoma, 2);
                // 更新大表的状态
                updatePackIm(pbList);
//                map.put("wlArr", wlArr);
//                map.put("qingxiChangStatus", 0);
//                Map<String,Object> map2 = new HashMap<>();
//                map2.put("",);
//                zsWlpackService.findAllWlpackListNoPage();
//                List<ZsQingxiMingxi> zsQingximingxiList = zsQingxiMingxiService.findAllQingxiMingxi(map);
//                for (ZsQingxiMingxi zsQingxiMingxi : zsQingximingxiList) {
//                    zsQingxiMingxi.setQingxiShenherenTiaoma(peibaoren);
//                }
//                if (zsQingximingxiList.size() != 0) {
//                    zsQingxiMingxiService.updateBatch(zsQingximingxiList);
//                }
                rtnMap.put("packageTiaoma", packageTiaoma);
                rtnMap.put("status", true);
                rtnMap.put("msg", "配包成功");
            } else {
                rtnMap.put("status", "false");
                rtnMap.put("msg", "配包人无效");
            }

        } catch (Exception e) {
            rtnMap.put("status", "false");
            rtnMap.put("msg", "失败");
            e.printStackTrace();
        }
        return rtnMap;
        //writeByMapString(rtnMap);
    }

    @RequestMapping("/dytiaoma.do")
    public ModelAndView dytiaoma(
            String huishouWupinbaoTiaoma, String huishouWupinbaoMuban, HttpServletRequest request,
            HttpServletResponse response
    ) throws BusinessException {
        request.setAttribute("huishouWupinbaoTiaoma", huishouWupinbaoTiaoma);

        request.setAttribute("huishouWupinbaoMuban", huishouWupinbaoMuban);
        return new ModelAndView("/peibao/peibao_dytiaoma");
    }

    /**
     * 更新大表的状态
     * <p>
     * //     * @param zsQingxi
     * //     * @param zsWlPacklist
     * //
     */
    private void updatePackIm(List<ZsPeibao> zsPeibaolist) throws BusinessException {
        List<ZsPackageImformation> packTmList = new ArrayList<>();
        for (ZsPeibao zsPeibao : zsPeibaolist) {
            ZsPackageImformation zsPackageImformation = new ZsPackageImformation();
//            ZsPackageImformation zspackage = zsPackageImService.selectByPrimaryKey(zsPeibao.getPeibaoBaozhuangTiaoma());
//            int liuzhuang = zspackage.getPackageLiuchengZhuangtai();
//            if (liuzhuang == 9) {
//                zsPackageImformation.setPackageLiuchengZhuangtai(10);
//            } else if (liuzhuang == 2) {
//                zsPackageImformation.setPackageLiuchengZhuangtai(3);
//            } else {
//
//            }

            zsPackageImformation.setPackageLiuchengZhuangtai(3);
            zsPackageImformation.setPackageTiaoma(zsPeibao.getPeibaoBaozhuangTiaoma());
            zsPackageImformation.setPeibaoLiushui(zsPeibao.getPeibaoLiushui());
            if (zsPeibao.getPeibaoHege() != null) {
                zsPackageImformation.setPeibaoHege(zsPeibao.getPeibaoHege());
            }

            if (zsPeibao.getPeibaoPiebaorenTiaoma() != null) {
                zsPackageImformation.setPeibaoPiebaorenTiaoma(zsPeibao.getPeibaoPiebaorenTiaoma());
            }

            if (zsPeibao.getPeibaoTime() != null) {
                zsPackageImformation.setPeibaoTime(zsPeibao.getPeibaoTime());
            }
            if (zsPeibao.getPeibaoShenherenTime() != null) {
                zsPackageImformation.setPeibaoShenherenTime(zsPeibao.getPeibaoShenherenTime());
            }
            if (zsPeibao.getPeibaoShenherenTiaoma() != null) {
                zsPackageImformation.setPeibaoShenherenTiaoma(zsPeibao.getPeibaoShenherenTiaoma());
            }
            if (zsPeibao.getPeibaoGuanbaoTime() != null) {
                zsPackageImformation.setPeibaoGuanbaoTime(zsPeibao.getPeibaoGuanbaoTime());
            }
            if (zsPeibao.getPeibaoGuanbaoTiaoma() != null) {
                zsPackageImformation.setPeibaoGuanbaoTiaoma(zsPeibao.getPeibaoGuanbaoTiaoma());
            }
            packTmList.add(zsPackageImformation);

        }
        if(packTmList.size()!=0){
            zsPackageImService.updateByBatch(packTmList);
        }

    }

    /**
     * @param packTiao 包的条码
     * @param status
     * @throws BusinessException
     */
    public void updateWlPackStatus(String[] packTiao, int status) throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        map.put("packTmArr", packTiao);
        List<ZsWanglanpackage> zsWllist = zsWlpackService.findAllWlpackListNoPage(map);
        String qingxiLiushui = zsWllist.get(0).getQingxiLiushui();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("qingxiLiushui", qingxiLiushui);
        int[] packageZhuangtaiArr = new int[]{0, 1};
        map2.put("packageZhuangtaiArr", packageZhuangtaiArr);

        for (ZsWanglanpackage zswl : zsWllist) {
            zswl.setPackageZhuangtai(status);
        }
        if (zsWllist.size() != 0) {
            zsWlpackService.updateBatch(zsWllist);
        }
        List<ZsWanglanpackage> zsWanglanpackages = zsWlpackService.findAllWlpackListNoPage(map2);
        int packageNum = zsWanglanpackages.size();
        //包的数量
        ZsQingxi zsQingxi = new ZsQingxi();
        zsQingxi.setQingxiLiushui(qingxiLiushui);
        zsQingxi.setPackageNum(packageNum);
        if (packageNum == 0) {
            zsQingxi.setPackageFlag(2);
        }
        zsQingxiService.updateByPrimaryKey(zsQingxi);

    }

    @RequestMapping("/piliangTiaoma")
    @ResponseBody
    public Map<String, Object> piliangTiaoma() {
        String list = request.getParameter("dataList");
        JSONArray jsonArray = JSONObject.parseArray(list);
        String[] peibaoBaozhuangTiaomas = new String[jsonArray.size()];
        String[] huishouWupinbaoMubans = new String[jsonArray.size()];
        List<String[]> list1 = new ArrayList<>();
        Map<String, Object> rtnMap = new HashMap<>();
        String shanchuMsg = "";
        boolean flag = true;
        try {
            for (int i = 0; i < jsonArray.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                peibaoBaozhuangTiaomas[i] = jsonObject.getString("packageTiaoma");
                huishouWupinbaoMubans[i] = jsonObject.getString("packId");
                map.put("peibaoBaozhuangTiaoma", peibaoBaozhuangTiaomas[i]);
                String baoId = huishouWupinbaoMubans[i];
                // int changeT = Integer.valueOf(changeTime);
                ZsPackageImformation zsPackageImformation = zsPackageImService.selectByPrimaryKey(peibaoBaozhuangTiaomas[i]);
                Date date = zsPackageImformation.getQingxiJieshuTime();
                TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
                if (tbPackageMuban != null) {
                    int print = tbPackageMuban.getPackagePrint();
                    String[] packImfor = new String[12];
                    if (print == 1) {
                        // int dyqxMing = tbPackageMuban.getPackagePrint();
                        List<ZsPeibao> peibaoList = zsPeibaoService.findAllZsPeibaoNoPage(map);
                        String peibaoren = "";
                        String shenheRen = "";
                        String guanbaoRen = "";
                        for (ZsPeibao peibao : peibaoList) {
                            peibaoren = peibao.getPeibaoPiebaorenTiaoma();
                            shenheRen = peibao.getPeibaoShenherenTiaoma();
                            guanbaoRen = peibao.getPeibaoGuanbaoTiaoma();
                        }
                        peibaoren = StartUpOnLoadData.emMap.get(peibaoren);
                        shenheRen = StartUpOnLoadData.emMap.get(shenheRen);
                        guanbaoRen = StartUpOnLoadData.emMap.get(guanbaoRen);
                        String baoTiaoma = peibaoBaozhuangTiaomas[i];
                        map.put("packageId", baoId);
                        String miejuntime = null;
                        String guoqitime = null;
                        long days = 0l;
                        if (tbPackageMuban != null) {
                            ZsDabao zsdabao = tbPackageMuban.getZsDabao();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            days = zsdabao.getDabaoYouxiaoqiDays();
                            //
                            if (date == null) {
                                date = zsPackageImformation.getPeibaoTime();
                            }
                            long time = 0l;
                            //东院
                            if(PathInfo.yiyuan==1){
                                time =  date.getTime() + (days-1) * 24l * 60l * 60l * 1000l;
                            }else if(PathInfo.yiyuan == 2){
                                time = date.getTime() + days * 24l * 60l * 60l * 1000l;
                            }
//
                            //宜昌

                            Date date1 = new Date();
                            date1.setTime(time);
                            miejuntime = sdf.format(date);
                            guoqitime = sdf.format(date1);
                        }
                        // 查询缺失
                        Map<String, Object> map2 = new HashMap<>();
                        map2.put("huishouWupinbaoTiaoma", baoTiaoma);
                        String queshi = "否";
                        String keshi = String.valueOf(zsPackageImformation.getPackageKeshi());
                        String depName = StartUpOnLoadData.depMap.get(keshi);
                        String baoName = tbPackageMuban.getPackageName();
                        String id = tbPackageMuban.getPackageUseno();
                        packImfor[1] = peibaoren;
                        packImfor[2] = depName;
                        packImfor[3] = baoName;
                        packImfor[4] = baoTiaoma;
                        packImfor[0] = queshi;
                        packImfor[6] = miejuntime;
                        packImfor[7] = guoqitime;
                        packImfor[8] = String.valueOf(days);
                        packImfor[9] = shenheRen;
                        packImfor[10] = guanbaoRen;
//
                        if("1".equals(id)){
                            packImfor[2] = "";
                        }
                        //外来 打印
                        String name = "";
                        if(tbPackageMuban.getPackageOutcategory()!=null){
                            int wailai = tbPackageMuban.getPackageOutcategory();
                            if(wailai==1){
                                Map<String,Object> map3 = new HashMap<>();
                                map3.put("packageId", baoId);
                                List<TbProduct> tbproductlist = tbProductService.findQixieByPackage(map3);

                                for(TbProduct tbProduct :tbproductlist){
                                    int num = 0;
                                    num = tbProduct.getNum();
                                    name += tbProduct.getProductName()+" "+num+" ";
                                }


                            }
                        }
                        packImfor[11] = name;

                        list1.add(packImfor);
                    }
                }else{
                    flag = false;
                    shanchuMsg += peibaoBaozhuangTiaomas[i];
                }

            }
            rtnMap.put("msg", "提交成功");
            rtnMap.put("status", "true");
            if(!flag){
                rtnMap.put("msg", shanchuMsg+"的录入模板包是被删除了");
                rtnMap.put("status", "shanchu");
            }

            rtnMap.put("packageList", list1);
        } catch (Exception e) {
            e.printStackTrace();
            rtnMap.put("msg", "打印失败");
            rtnMap.put("status", "false");
        }

        return rtnMap;
    }


    @RequestMapping("/tiaoma.do")
    @ResponseBody
    public Map<Object, Object> tiaoma(
            String huishouWupinbaoTiaoma, String huishouWupinbaoMuban, HttpServletRequest request,
            HttpServletResponse response
    ) throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        map.put("peibaoBaozhuangTiaoma", huishouWupinbaoTiaoma);
        String baoId = huishouWupinbaoMuban;
        // int changeT = Integer.valueOf(changeTime);
        ZsPackageImformation zsPackageImformation = zsPackageImService.selectByPrimaryKey(huishouWupinbaoTiaoma);
        Date date = zsPackageImformation.getQingxiJieshuTime();
        TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
        int print = tbPackageMuban.getPackagePrint();
        String[] packImfor = new String[11];
        if (print == 1) {
            // int dyqxMing = tbPackageMuban.getPackagePrint();
            List<ZsPeibao> peibaoList = zsPeibaoService.findAllZsPeibaoNoPage(map);
            String peibaoren = "";
            String shenheRen = "";
            String guanbaoRen = "";
            for (ZsPeibao peibao : peibaoList) {
                peibaoren = peibao.getPeibaoPiebaorenTiaoma();
                shenheRen = peibao.getPeibaoShenherenTiaoma();
                guanbaoRen = peibao.getPeibaoGuanbaoTiaoma();
            }
            peibaoren = StartUpOnLoadData.emMap.get(peibaoren);
            shenheRen = StartUpOnLoadData.emMap.get(shenheRen);
            guanbaoRen = StartUpOnLoadData.emMap.get(guanbaoRen);
            String baoTiaoma = huishouWupinbaoTiaoma;

            // if(changeT==0){
            // Map<String ,Object> map = new HashMap<String,Object>();
            // map.put("packageId", baoId);
            // List<ZsDabao> zsBao = zsDabaoService.findAllDabaoNoPage(map);
            // for(ZsDabao zsdabao:zsBao){
            // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // Date date = new Date();
            // int days = zsdabao.getDabaoYouxiaoqiDays();
            // long time = date.getTime()+days*24*60*60*1000;
            // date.setTime(time);
            // String miejuntime = sdf.format(new Date());
            // String guoqitime = sdf.format(date);
            // request.setAttribute("miejuntime", miejuntime);
            // request.setAttribute("guoqitime", guoqitime);
            // request.setAttribute("days", days);
            // }
            // }else{
            //
            // }

            map.put("packageId", baoId);
            String miejuntime = null;
            String guoqitime = null;
            long days = 0l;
            if (tbPackageMuban != null) {
                ZsDabao zsdabao = tbPackageMuban.getZsDabao();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                days = zsdabao.getDabaoYouxiaoqiDays();
                //
                if (date == null) {
                    date = zsPackageImformation.getHuishouTime();
                }
                long time = date.getTime() + days * 24l * 60l * 60l * 1000l;
                Date date1 = new Date();
                date1.setTime(time);
                miejuntime = sdf.format(date);
                guoqitime = sdf.format(date1);
//                request.setAttribute("miejuntime", miejuntime);
//                request.setAttribute("guoqitime", guoqitime);
//                request.setAttribute("days", days);
            }
//            zsDabaoService.findAllDabaoNoPage(map);

//            for (ZsDabao zsdabao : zsBao) {
//
//            }

            // 查询缺失
            Map<String, Object> map2 = new HashMap<>();
            map2.put("huishouWupinbaoTiaoma", baoTiaoma);
//            List<ZsHuishouQueshi> zsHuishouQueshilist = zsHuishouQueshiService.findAllZsHuishouQueshi(map2);
//            request.setAttribute("zsHuishouQueshilist", zsHuishouQueshilist);

            // if(dyqxMing==0){
            // }else{
            // Map<String,Object> map3 = new HashMap<String,Object>();
            // map3.put("packageId", baoId);
            // List<TbProduct> tbproductlist =
            // tbProductService.findQixieByPackage(map3);
            // request.setAttribute("tbproductlist", tbproductlist);
            // }
            String queshi = "否";
//            if (zsHuishouQueshilist.size() != 0) {
//                queshi = "是";
//                request.setAttribute("queshi", true);
//            } else {
//                queshi = "否";
//            }
            String keshi = String.valueOf(zsPackageImformation.getPackageKeshi());
            String depName = StartUpOnLoadData.depMap.get(keshi);
            String baoName = tbPackageMuban.getPackageName();
            packImfor[1] = peibaoren;
            packImfor[2] = depName;
            packImfor[3] = baoName;
            packImfor[4] = baoTiaoma;
            packImfor[0] = queshi;
            packImfor[6] = miejuntime;
            packImfor[7] = guoqitime;
            packImfor[8] = String.valueOf(days);
            packImfor[9] = shenheRen;
            packImfor[10] = guanbaoRen;
        }

        //request.setAttribute("packImfor", JSON.toJSON(packImfor));
        Map<Object, Object> rtnMap = new HashMap<>();
        rtnMap.put("msg", "提交成功");
        rtnMap.put("print", print);
        rtnMap.put("status", "true");
        rtnMap.put("packImfor", packImfor);
        return rtnMap;
        //return success(packImfor);
        //writeByMapString(rtnMap);
        //return new ModelAndView("/peibao/peibao_tiaoma");
    }

    /**
     * 审核界面的清单展示
     *
     * @param request
     * @param response
     * @return
     * @throws BusinessException
     */
    @RequestMapping("shenheList.do")
    public ModelAndView shenheList(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1
                : Integer.valueOf(request.getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 30);
        map.put("peibaoZhuangtai", 0);
        int[] hegeArr = new int[]{0, 2};
        map.put("peibaoHege", hegeArr);
        PageInfo<ZsPeibao> page = zsPeibaoService.findAllZsPeibao(map);
        request.setAttribute("emMap", StartUpOnLoadData.emMap);
        request.setAttribute("peibaoList", page.getList());
        request.setAttribute("total", page.getPages());
        return new ModelAndView("/peibao/peibao_shenhe");
    }

    @RequestMapping("guanbaoList.do")
    public ModelAndView guanbaoList(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1
                : Integer.valueOf(request.getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 30);
        map.put("peibaoZhuangtai", 0);
        int[] hegeArr = new int[]{1};
        map.put("peibaoHege", hegeArr);
        PageInfo<ZsPeibao> page = zsPeibaoService.findAllZsPeibao(map);
        request.setAttribute("emMap", StartUpOnLoadData.emMap);
        request.setAttribute("peibaoList", page.getList());
        request.setAttribute("total", page.getPages());
        return new ModelAndView("/peibao/peibao_guanbao");
    }

    @RequestMapping("/guanbaoBaocun.do")
    @ResponseBody
    public Map<Object, Object> guanbaoBaocun(String pdata,
                                             String[] peibaoLiushui, String guanbaoTiaoma, String[] peibaoBaozhuangTiaoma, HttpServletRequest request,
                                             HttpServletResponse response
    ) throws BusinessException {

        if (StringUtils.isNotBlank(pdata)) {
            JSONObject jsonObject = JSONObject.parseObject(pdata);
            JSONArray datas = jsonObject.getJSONArray("datas");
            guanbaoTiaoma = jsonObject.getString("peoplecode");
            peibaoLiushui = new String[datas.size()];
            peibaoBaozhuangTiaoma = new String[datas.size()];
            for (int i = 0; i < datas.size(); i++) {
                String packageTiaoma = JSONObject.parseObject(datas.get(i).toString()).getString("peibaoBaozhuangTiaoma");
                String hege = JSONObject.parseObject(datas.get(i).toString()).getString("state");
                String liushui = JSONObject.parseObject(datas.get(i).toString()).getString("peibaoLiushui");
                peibaoLiushui[i] = liushui;
                peibaoBaozhuangTiaoma[i] = packageTiaoma;
            }
        }
        Map<Object, Object> rtnMap = new HashMap<>();
        TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(guanbaoTiaoma);
        if (tbEmployee != null) {
            try {
                ZsPeibao zsPeibao = null;
                Date date = new Date();
                List<ZsPeibao> zsPeibaoList = new ArrayList<>();
                for (int i = 0; i < peibaoLiushui.length; i++) {
                    zsPeibao = new ZsPeibao();
                    zsPeibao.setPeibaoLiushui(peibaoLiushui[i]);
                    zsPeibao.setPeibaoGuanbaoTiaoma(guanbaoTiaoma.toUpperCase());
                    zsPeibao.setPeibaoGuanbaoTime(date);
                    zsPeibao.setPeibaoZhuangtai(1);
                    zsPeibao.setPeibaoBaozhuangTiaoma(peibaoBaozhuangTiaoma[i]);
                    zsPeibaoList.add(zsPeibao);
                }
                zsPeibaoService.updateBatch(zsPeibaoList);
                // 更新大表的状态
                updatePackIm(zsPeibaoList);
                rtnMap.put("status", "true");
                rtnMap.put("msg", "成功");
            } catch (Exception e) {
                rtnMap.put("status", "false");
                rtnMap.put("msg", "失败");
                e.printStackTrace();
            }
        } else {
            rtnMap.put("status", "false");
            rtnMap.put("msg", "无效的关包人条码");
        }

        return rtnMap;

    }

    @RequestMapping("/peibaoshenheBaocun.do")
    @ResponseBody
    public Map<Object, Object> qingxiShenhe(
            String pdata,
            String[] peibaoHege, String[] peibaoLiushui, String peibaoShenherenTiaoma, String[] peibaoBaozhuangTiaoma,
            HttpServletRequest request, HttpServletResponse response
    ) throws BusinessException {

        if (StringUtils.isNotBlank(pdata)) {
            JSONObject jsonObject = JSONObject.parseObject(pdata);
            JSONArray datas = jsonObject.getJSONArray("datas");
            peibaoShenherenTiaoma = jsonObject.getString("peoplecode");
            peibaoLiushui = new String[datas.size()];
            peibaoHege = new String[datas.size()];
            peibaoBaozhuangTiaoma = new String[datas.size()];
            for (int i = 0; i < datas.size(); i++) {
                String packageTiaoma = JSONObject.parseObject(datas.get(i).toString()).getString("peibaoBaozhuangTiaoma");
                String hege = JSONObject.parseObject(datas.get(i).toString()).getString("state");
                String liushui = JSONObject.parseObject(datas.get(i).toString()).getString("peibaoLiushui");
                peibaoHege[i] = hege;
                peibaoLiushui[i] = liushui;
                peibaoBaozhuangTiaoma[i] = packageTiaoma;
            }
        }
        Map<Object, Object> rtnMap = new HashMap<>();
        TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(peibaoShenherenTiaoma);
        if (peibaoHege != null) {
            if (tbEmployee != null) {
                try {
                    ZsPeibao zsPeibao = null;
                    Integer hege;
                    List<ZsPeibao> zsPeibaoList = new ArrayList<>();
                    for (int i = 0; i < peibaoHege.length; i++) {
                        hege = Integer.valueOf(peibaoHege[i]);
                        zsPeibao = new ZsPeibao();
                        zsPeibao.setPeibaoHege(hege);
                        zsPeibao.setPeibaoLiushui(peibaoLiushui[i]);
                        zsPeibao.setPeibaoShenherenTiaoma(peibaoShenherenTiaoma.toUpperCase());
                        zsPeibao.setPeibaoBaozhuangTiaoma(peibaoBaozhuangTiaoma[i]);
                        zsPeibao.setPeibaoShenherenTime(new Date());
                        zsPeibaoList.add(zsPeibao);

                    }
                    zsPeibaoService.updateBatch(zsPeibaoList);
                    // 更新大表的状态
                    updatePackIm(zsPeibaoList);
                    rtnMap.put("status", "true");
                    rtnMap.put("msg", "审核成功");
                } catch (Exception e) {
                    rtnMap.put("status", "false");
                    rtnMap.put("msg", "审核失败");
                    e.printStackTrace();
                }
            } else {
                rtnMap.put("status", "false");
                rtnMap.put("msg", "无效的审核人条码");
            }
        } else {
            rtnMap.put("status", "false");
            rtnMap.put("msg", "无数据");
        }


        return rtnMap;
    }

    @RequestMapping("/peibaoList")
    public ModelAndView peibaoList(
            String searchTxt, String st, String et, HttpServletRequest request, HttpServletResponse response
    ) throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1
                : Integer.valueOf(request.getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        if (StringUtils.isNotBlank(searchTxt)) {
            map.put("searchTxt", searchTxt);
        }
        try {
            if (StringUtils.isNotBlank(st)) {
                startTime = sdf.parse(st);
                map.put("startTime", startTime);
            }
            if (StringUtils.isNotBlank(et)) {
                endTime = sdf.parse(et);
                map.put("endTime", endTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        map.put("qingxiZhuangtai", 2);
        map.put("qingxiHege", 1);
        PageInfo<ZsQingxi> page = zsQingxiService.findAllZsQingxi(map);
        request.setAttribute("qingxiList", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        request.setAttribute("emMap", StartUpOnLoadData.emMap);
        request.setAttribute("equipMap", StartUpOnLoadData.equipMap);
        request.setAttribute("softMap", StartUpOnLoadData.softMap);
        return new ModelAndView("/peibao/peibaoList");
    }

    @RequestMapping("/peibaoAdd")
    public ModelAndView peibaoAdd() {
        Map<String, Object> map = new HashMap<>();
//        map.put("packageDepartid",15);
        map.put("packageType", 1);
        List<TbPackageMuban> list = tbPackageMubanService.findPackage(map);
        request.setAttribute("packages", list);
        request.setAttribute("depMap", StartUpOnLoadData.depMap);
        return new ModelAndView("/peibao/peibao_adpackage");
    }


}
